<template>
  <l-data-panel v-if="data._isInit" :label="config.title">
    <l-echarts ref="main" :option="option"></l-echarts>
  </l-data-panel>
</template>
<script>
import mixin from "../chart";
export default {
  mixins: [mixin()],
  data() {
    return {
      option: {
        tooltip: {
          trigger: "item",
        },
        series: [
          {
            type: "funnel",
            label: {},
            itemStyle: {},
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      },
    };
  },
  methods: {
    getOption() {
      const { labelKey, valueKey } = this.config;

      // 数据配置
      const legendData = [];
      const data = this.showData || [];
      if (labelKey && valueKey) {
        const serieData = [];
        data.forEach((t) => {
          serieData.push({ name: t[labelKey], value: t[valueKey], list: data });
          // 图例数据
          const legendItem = { name: t[labelKey] };
          if (this.legend.itemIcon) {
            legendItem.icon = this.legend.itemIcon;
          }
          legendData.push(legendItem);
        });
        this.option.series[0].data = serieData;

        // 为了实现进度环而做的特殊设置
        if (!this.label.show && this.label.lastShow && serieData.length > 0) {
          serieData[serieData.length - 1].label = { show: true };
        }
      }

      const serie = this.option.series[0];
      this.setFunnel(serie, this.series.funnel);
      // 设置样式
      const { itemStyle } = this.series;
      serie.itemStyle = itemStyle;

      // 文本标签设置
      serie.label = this.getLabel(this.label);

      // 图例设置
      this.option.legend = this.getLegend(this.option.series[0].data);
      this.option.legend.data = legendData;

      // 颜色设置
      const color = this.getColors(this.echarts);
      if (color && color.length > 0) {
        this.option.color = color;
      }

      return this.option;
    },
    setFunnel(
      serie,
      { min, max, minSize, maxSize, left, width, top, bottom, sort, gap }
    ) {
      serie.min = min;
      serie.max = max;
      serie.minSize = minSize + "%";
      serie.maxSize = maxSize + "%";
      serie.left = left + "%";
      serie.top = top;
      serie.bottom = bottom;
      serie.width = width + "%";
      serie.sort = sort;
      serie.gap = gap;
    },
  },
};
</script>

